package com.huawei.appgallery.assistantdock.base.service;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.NonNull;
import com.huawei.appgallery.assistantdock.base.service.base.dispatch.GameServiceMethod;
import com.huawei.appgallery.assistantdock.base.service.bean.BuoyResponse;
import com.huawei.appgallery.assistantdock.buoydock.bean.GameBuoyRedInfoBean;
import com.huawei.appgallery.foundation.account.bean.UserSession;
import com.huawei.gamebox.plugin.gameservice.bean.GameInfo;
import com.huawei.gamebox.plugin.gameservice.bean.GetGameBuoyEntryInfoResultResp;
import com.huawei.gamebox.plugin.gameservice.service.RequestInfo;
import o.beo.f;
import o.bfg;
import o.bfk;
import o.bfl;
import o.bfm;
import o.bfs;
import o.bfu;
import o.bfu.d;
import o.bfv;
import o.bga;
import o.bgd;
import o.bgi;
import o.big;
import o.AccountBussinessListener;
import o.ccp;
import o.dex.a;
import o.diu;
import o.Logger;
import o.dof;
import o.doo;
import o.eod;
import o.eoj;
import o.ezo;
import o.ezs;
import o.ezv;
import o.ezx;
import o.faa;
import o.fad;
import o.fae;
import o.fam;
import org.json.JSONException;
import org.json.JSONObject;

public class BuoyServiceDispatcher extends BaseGameServiceDispatcher implements fae {
    private static final int HIDE_RED_DOT = 1;
    private static final String METHOD_SWITCH_ACCOUNT = "switchGameSubAcct";
    private static final int SDK_VERSION_CODE_731 = 70301300;
    private static final int SHOW_RED_DOT = 0;
    private static final String TAG = "BuoyServiceDispatcher";
    private String buoyOperationId = null;
    protected bfm params;
    private long startTime = 0;

    interface c {
        void ˋ();
    }

    public void dispatch(Context context, @NonNull RequestInfo requestInfo, fad o_fad) {
        GameInfo gameInfo = new GameInfo();
        gameInfo.setSdkVersionCode(requestInfo.ˋ());
        gameInfo.setSdkVersionName(requestInfo.ˏ());
        gameInfo.setCpId(requestInfo.ˊ());
        gameInfo.setPackageName(requestInfo.ॱॱ());
        gameInfo.setAppId(requestInfo.ˎ());
        if (getSdkVersionCode(gameInfo) >= SDK_VERSION_CODE_731) {
            ezv.ॱ().ˎ(context, gameInfo, METHOD_SWITCH_ACCOUNT, o_fad);
        } else {
            ezv.ॱ().ॱ(gameInfo);
        }
        bga.ॱ().ˋ(gameInfo, o_fad);
        if (GameServiceMethod.METHOD_SHOW_BUOY_DIALOG.equals(requestInfo.ॱ())) {
            parseParams(requestInfo);
        }
        super.dispatch(context, requestInfo, o_fad);
    }

    private void parseParams(@NonNull RequestInfo requestInfo) {
        if (!TextUtils.isEmpty(requestInfo.ʻ())) {
            if (this.params == null) {
                this.params = new bfm();
            }
            try {
                JSONObject jSONObject = new JSONObject(requestInfo.ʻ());
                int optInt = jSONObject.optInt("buoyLocation", 0);
                a aVar = a.ˏ;
                if (optInt == a.ˋ.ˋ()) {
                    aVar = a.ˋ;
                }
                this.params.ˏ(aVar);
                this.params.ˎ(jSONObject.optBoolean("isFromDockCircle", false));
            } catch (JSONException e) {
                Logger.e(TAG, "get params error JSONException");
            }
        }
    }

    public bfm getRequestParams() {
        return this.params;
    }

    @PermissionGuard
    public void showBuoyDialog() {
        this.startTime = System.currentTimeMillis();
        checkLogin(new c(this) {
            final /* synthetic */ BuoyServiceDispatcher ˋ;

            {
                this.ˋ = r1;
            }

            public void ˋ() {
                this.ˋ.openBuoyWindow();
            }
        });
    }

    public void finishBuoyDialog() {
        new Handler(Looper.getMainLooper()).post(new Runnable(this) {
            final /* synthetic */ BuoyServiceDispatcher ˏ;

            {
                this.ˏ = r1;
            }

            public void run() {
                this.ˏ.buoyOperationId = this.ˏ.mReqId;
                fam.ॱ().ˋ(this.ˏ);
                fam.ॱ().ˏ(this.ˏ.mContext);
            }
        });
    }

    public void getBuoyRedInfo() {
        if (diu.ˊ().ˎ()) {
            new bfu(this.mContext, getGameInfo()).ˏ(new d(this) {
                final /* synthetic */ BuoyServiceDispatcher ˎ;

                {
                    this.ˎ = r1;
                }

                public void ॱ(boolean z) {
                    this.ˎ.sendRedDotResult(z);
                }
            });
        } else {
            sendRedDotResult(true);
        }
    }

    public void showBuoyGuide() {
        fam.ॱ().ˋ(this);
        bgd.ॱ().ॱ(this.mContext, this.mCallback);
    }

    public void getBuoyNewRedNotice() {
        if (diu.ˊ().ˎ()) {
            new bfv(this.mContext, getGameInfo()).ˊ(new o.bfv.c(this) {
                final /* synthetic */ BuoyServiceDispatcher ˎ;

                {
                    this.ˎ = r1;
                }

                public void ˏ(int i) {
                    this.ˎ.sendNewRedNoticeResult(i);
                }
            });
        } else {
            sendNewRedNoticeResult(0);
        }
    }

    public void getGameBuoyEntryInfo() {
        if (diu.ˊ().ˎ()) {
            faa o_faa = new faa(this.mGameInfo);
            o_faa.ˎ(new faa.a(this) {
                final /* synthetic */ BuoyServiceDispatcher ˊ;

                {
                    this.ˊ = r1;
                }

                public void ˎ(Boolean bool) {
                    if (this.ˊ.mCallback != null) {
                        GetGameBuoyEntryInfoResultResp getGameBuoyEntryInfoResultResp = new GetGameBuoyEntryInfoResultResp();
                        getGameBuoyEntryInfoResultResp.ˏ(bool.booleanValue());
                        try {
                            this.ˊ.mCallback.ˋ(this.ˊ.mReqId, getGameBuoyEntryInfoResultResp.toJson());
                        } catch (Throwable e) {
                            Logger.ˋ(BuoyServiceDispatcher.TAG, "send the response to client exception", e);
                        } catch (Throwable e2) {
                            Logger.ˋ(BuoyServiceDispatcher.TAG, "To buoy entry result json failed", e2);
                        } catch (Throwable e22) {
                            Logger.ˋ(BuoyServiceDispatcher.TAG, "To buoy entry result json failed", e22);
                        }
                    }
                }
            });
            o_faa.ॱ();
        }
    }

    private void sendRedDotResult(boolean z) {
        if (this.mCallback == null) {
            if (Logger.ˎ()) {
                Logger.ˏ(TAG, "callback is null");
            }
        } else if (!dof.ˋ(this.mReqId)) {
            GameBuoyRedInfoBean gameBuoyRedInfoBean = new GameBuoyRedInfoBean();
            gameBuoyRedInfoBean.ˎ(Integer.valueOf(z ? 0 : 1));
            if (Logger.ˎ()) {
                Logger.ˏ(TAG, "red dot state :" + gameBuoyRedInfoBean.ˏ());
            }
            try {
                this.mCallback.ˋ(this.mReqId, gameBuoyRedInfoBean.toJson());
            } catch (Exception e) {
                Logger.e(TAG, "String to Json exception");
            }
        } else if (Logger.ˎ()) {
            Logger.ˏ(TAG, "method is blank");
        }
    }

    private void sendNewRedNoticeResult(int i) {
        if (this.mCallback == null) {
            if (Logger.ˎ()) {
                Logger.ˏ(TAG, "callback is null");
            }
        } else if (!dof.ˋ(this.mReqId)) {
            GameBuoyRedInfoBean gameBuoyRedInfoBean = new GameBuoyRedInfoBean();
            gameBuoyRedInfoBean.ˏ(Integer.valueOf(i));
            if (Logger.ˎ()) {
                Logger.ˏ(TAG, "has new red msg :" + gameBuoyRedInfoBean.ॱ());
            }
            try {
                this.mCallback.ˋ(this.mReqId, gameBuoyRedInfoBean.toJson());
            } catch (Exception e) {
                Logger.e(TAG, "String to Json exception");
            }
        } else if (Logger.ˎ()) {
            Logger.ˏ(TAG, "method is blank");
        }
    }

    public void destroy() {
        super.destroy();
        bfk.ˊ().ॱ(getGameInfo());
        fam.ॱ().ˋ(null);
        new Handler(Looper.getMainLooper()).post(new Runnable(this) {
            final /* synthetic */ BuoyServiceDispatcher ˊ;

            {
                this.ˊ = r1;
            }

            public void run() {
                fam.ॱ().ॱ(this.ˊ.mContext, false);
                if (this.ˊ.mCallback != null) {
                    try {
                        this.ˊ.mCallback.ˋ(GameServiceMethod.METHOD_FINISH_BUOY_DIALOG, new BuoyResponse(0, null).toJson());
                    } catch (Throwable e) {
                        Logger.ˋ(BuoyServiceDispatcher.TAG, "send the finish result to sdk failed", e);
                    }
                }
            }
        });
    }

    public void unbind() {
        super.unbind();
        new Handler(Looper.getMainLooper()).post(new Runnable(this) {
            final /* synthetic */ BuoyServiceDispatcher ˏ;

            {
                this.ˏ = r1;
            }

            public void run() {
                fam.ॱ().ˋ(null);
                fam.ॱ().ॱ(this.ˏ.mContext);
            }
        });
    }

    public void onShow() {
        if (this.mCallback != null) {
            try {
                bfk.ˊ().ˏ(System.currentTimeMillis());
                this.mCallback.ˋ(GameServiceMethod.METHOD_SHOW_BUOY_DIALOG, new BuoyResponse(0, null).toJson());
            } catch (Throwable e) {
                Logger.ˋ(TAG, "send the show result to sdk failed", e);
            }
        }
        long currentTimeMillis = System.currentTimeMillis();
        if (currentTimeMillis - this.startTime > 0) {
            bfg.ॱ(currentTimeMillis - this.startTime);
        }
        bfg.ˋ(true);
    }

    public void onClose() {
        bgi.ˋ().ˏ("GS0020001_" + UserSession.getInstance().getUserId());
        if (this.mCallback != null) {
            try {
                bfk.ˊ().ॱ(getGameInfo());
                this.mCallback.ˋ(this.buoyOperationId, new BuoyResponse(2, null).toJson());
            } catch (Throwable e) {
                Logger.ˋ(TAG, "send the show result to sdk failed", e);
            }
        }
    }

    public void onHide() {
        onClose();
    }

    public void onFail(String str) {
        if (this.mCallback != null) {
            try {
                this.mCallback.ˋ(this.buoyOperationId, new BuoyResponse(1, str).toJson());
            } catch (Throwable e) {
                Logger.ˋ(TAG, "send the show result to sdk failed", e);
            }
        }
        bfg.ˋ(false);
    }

    public GameInfo getGameInfo() {
        if (this.mGameInfo != null && dof.ॱ(this.mGameInfo.getAppId())) {
            ezo ˋ = ezs.ˏ().ˋ(this.mGameInfo);
            if (ˋ != null) {
                this.mGameInfo.setAppId(ˋ.ˊ());
            }
        }
        return this.mGameInfo;
    }

    public void notify(String str, String str2) {
        if (this.mCallback != null) {
            try {
                this.mCallback.ˋ(str, str2);
            } catch (Throwable e) {
                Logger.ˋ(TAG, "send the show result to sdk failed", e);
            }
        }
    }

    private void checkLogin(final c cVar) {
        if (!doo.ˊ(this.mContext)) {
            Log.e(TAG, "BuoyServiceDispatcher hasNoActiveNetwork");
            enterOfflineMode(getGameInfo());
        } else if (!UserSession.getInstance().isLoginSuccessful() || UserSession.getInstance().getStatus() == 1) {
            eoj.ˋ().ˎ(TAG, new AccountBussinessListener(this) {
                final /* synthetic */ BuoyServiceDispatcher ॱ;

                public void onAccountBusinessResult(ccp o_ccp) {
                    if (o_ccp.ˎ == 102) {
                        eod.ˊ(this.ॱ.mContext);
                    }
                    cVar.ˋ();
                    eoj.ˋ().ˊ(BuoyServiceDispatcher.TAG);
                }
            });
            bfs.ˋ().ˎ(this.mContext);
        } else {
            cVar.ˋ();
        }
    }

    private void enterOfflineMode(GameInfo gameInfo) {
        Logger.ˏ(TAG, "enterOfflineMode");
        if (!fam.ॱ().ʻ()) {
            if (Logger.ˎ()) {
                Logger.ˏ(TAG, "!BuoyWindowManager.getInstance().isEmpty()");
            }
            openBuoyWindow();
        } else if (big.ॱ(gameInfo)) {
            openBuoyWindow();
        } else {
            showNoNetworkToast();
        }
    }

    private void openBuoyWindow() {
        this.buoyOperationId = this.mReqId;
        fam.ॱ().ˋ(this);
        new Handler(Looper.getMainLooper()).post(new bfl(this.params, this.mContext));
    }

    private void showNoNetworkToast() {
        new Handler(Looper.getMainLooper()).post(new Runnable(this) {
            final /* synthetic */ BuoyServiceDispatcher ˊ;

            {
                this.ˊ = r1;
            }

            public void run() {
                if (this.ˊ.mContext != null) {
                    ezx.ˎ().ˊ(this.ˊ.mContext.getResources().getString(f.ﹳ));
                }
            }
        });
    }

    private int getSdkVersionCode(@NonNull GameInfo gameInfo) {
        int i = 0;
        try {
            i = Integer.parseInt(gameInfo.getSdkVersionCode());
        } catch (NumberFormatException e) {
            Logger.e(TAG, "the version code is not number format");
        }
        return i;
    }
}
